<template>
  <div class="header">
    <router-link tag="div" to="/" v-show="showAbs" class="header-abs"><span class="iconfont icon-back"></span></router-link>
    <div class="header-fixed" v-show="!showAbs" :style="opacityStyle">
      <router-link to="/">
        <div class="iconfont icon-back"></div>
      </router-link>
      景点详情
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showAbs: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  mounted () {
    window.addEventListener('scroll', this.onScroll)
  },
  destroyed () {
    window.removeEventListener('scroll', this.onScroll)
  },
  activated () {
    window.addEventListener('scroll', this.onScroll)
  },
  deactivated () {
    window.removeEventListener('scroll', this.onScroll)
  },
  methods: {
    onScroll () {
      const top = document.documentElement.scrollTop
      if (top > 60) {
        this.showAbs = false
        this.opacityStyle = {
          opacity: top > 140 ? 1 : top / 140
        }
      } else {
        this.showAbs = true
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~@/assets/styles/varibles.styl'
  .header-abs
    display flex
    align-items center
    justify-content center
    position absolute
    top .2rem
    left .2rem
    width .8rem
    height .8rem
    border-radius .4rem
    background rgba(0, 0, 0, .8)
    .iconfont
      font-size .24rem
      color #ffffff
  .header-fixed
    position fixed
    top 0
    left 0
    right 0
    z-index 2
    height $headerHeight
    line-height $headerHeight
    text-align center
    color #ffffff
    background $bgColor
    font-size .32rem
    .icon-back
      position absolute
      top 0
      left 0
      width .64rem
      height 100%
      font-size .32rem
      color #ffffff
</style>
